#define _CRT_SECURE_NO_WARNINGS 1
#include"Head.h"
struct Com
{
    bool operator()(const pair<int,string>& l,const pair<int,string>& r)
    {
        return l.first>r.first||(l.first==r.first&&l.second<r.second);
    }
};
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string,int> fre;
        for(auto& e:words)
        {
            fre[e]++;
        }
        vector<pair<int,string>> v;
        for(auto& kv:fre)
        {
            v.push_back(make_pair(kv.second,kv.first));
        }
        //stable_sort(v.begin(),v.end(),Com());
        sort(v.begin(),v.end(),Com());
        vector<string> ans;
        for(int i=0;i<k;i++)
        {
            ans.push_back(v[i].second);
        }
        return ans;
    }
};